import { fileURLToPath, URL } from 'node:url';
import { defineConfig } from 'vite';
import { quasar, transformAssetUrls } from '@quasar/vite-plugin';
import { QuasarResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
// import jsconfig from './jsconfig.json';

// const head = xs => xs[0];
// const removeAsterisk = s => s.slice(0, -2);
// const toPath = s => fileURLToPath(new URL(s, import.meta.url));
// const AliasPair = pair => ({
//   // 1. 取 path 数组下标 0 的
//   // 2. 去掉 '/*'
//   // 3. 转绝对路径
//   [removeAsterisk(pair[0])]: toPath( removeAsterisk(head(pair[1])) )
// });

// console.log(
//   Object
//   .entries(jsconfig.compilerOptions.paths)
//   .reduce(
//     (acc, pair) => Object.assign(acc, AliasPair(pair)),
//     {}
//   )
// )

// console.log(
//   '@:', fileURLToPath(new URL('src', import.meta.url)),
//   'video-action:', fileURLToPath(new URL('src/views/video-action', import.meta.url))
// );

export default defineConfig({
  plugins: [
    vue({
      template: { transformAssetUrls }
    }),
    Components({
      resolvers: [QuasarResolver()]
    }),
    quasar({
      autoImportComponentCase: 'pascal'
    }),
    vueJsx(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      'video-action': fileURLToPath(new URL('./src/views/video-action', import.meta.url)),
    },
    // alias: (
    //   Object
    //   .entries(jsconfig.compilerOptions.paths)
    //   .reduce(
    //     (acc, pair) => Object.assign(acc, AliasPair(pair)),
    //     {}
    //   )
    // ),
  },
  server: {
    host: '0.0.0.0',
    proxy: {
      '/api': {
        // target: 'http://192.168.21.51:7758',
        // target: 'http://192.168.21.51:8527',
        // target: 'http://192.168.100.254:7758/',
        // target: 'http://218.26.98.206:59019/',
        target: 'http://192.168.121.21:9019/',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/api/, ''),
      },
      '/upload': {
        target: 'http://192.168.100.47:8080/group1/upload',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/upload/, ''),
      },
    },
  },
  build: {
    rollupOptions: {
      output: {
        chunkFileNames: `assets/[name]-[hash]-${Date.now()}.js`,
        entryFileNames: `assets/[name]-[hash]-${Date.now()}.js`,
        assetFileNames: `assets/[ext]/[name]-[hash]-${Date.now()}.[ext]`
      },
    },
  },
});
